﻿using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Umbraco.Cms.Web.EmbeddedViewEngine;
using Umbraco.Cms.Web.Model.BackOffice.PropertyEditors;
using Umbraco.Framework;

namespace Umbraco.Cms.Web.PropertyEditors.UserPicker
{
	public class UserPickerPreValueModel : PreValueModel
	{
		[ScaffoldColumn(false)]
		public IEnumerable<HiveId> SelectedUserGroups { get; set; }

		[DisplayName("User Groups")]
		[EmbeddedView("Umbraco.Cms.Web.PropertyEditors.UserPicker.Views.UserPickerUserGroups.cshtml", "Umbraco.Cms.Web.PropertyEditors")]
		public IEnumerable<SelectListItem> UserGroups { get; set; }

		public override string GetSerializedValue()
		{
			return (new JavaScriptSerializer()).Serialize(this);
		}

		public override void SetModelValues(string serializedVal)
		{
			if (string.IsNullOrEmpty(serializedVal))
				return;

			var deserialized = (new JavaScriptSerializer()).Deserialize<UserPickerPreValueModel>(serializedVal);

			// set values
			this.SelectedUserGroups = deserialized.SelectedUserGroups;
		}
	}
}